package com.example.arithmeticleetcode.leetcode;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description: 5392. 分割字符串的最大得分
 * @author: FangZhen
 * @create: 2020-04-27 17:27
 **/
public class Demo08 {

    public static void main(String[] args) {
        int i1 = maxScore("011101");//5
        int i2 = maxScore("011101");//5
        int i3 = maxScore("1111");//3
        int i4 = maxScore("0100");//2
        System.out.println(i1);
        System.out.println(i2);
        System.out.println(i3);
        System.out.println(i4);
    }

    public static int maxScore(String s) {
        Map<Character, Integer> map = new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }
        if (!map.containsKey('0') || !map.containsKey('1')) {
            return s.length() - 1;
        }
        List<Integer> list = new ArrayList<>();
        list.add(0);
        int zeroCount = 0;
        int oneCount = 0;
        for (int i = 0, len = s.length(); i < len - 1; i++) {
            if (s.charAt(i) == '0') {
                zeroCount++;
            } else {
                oneCount++;
            }
            list.add(zeroCount + map.get('1') - oneCount);
        }
        Collections.sort(list);
        return list.get(list.size() - 1);
    }

    public static int goodCode(String s) {
        int num0 = 0, num1 = 0, max = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '0')
                num0++;
            else
                num1++;
        }
        int count0 = 0, count1 = num1;
        for (int i = 0; i < s.length() - 1; i++) {
            if (s.charAt(i) == '0') {
                count0++;
            } else
                count1--;
            max = Math.max(max, count0 + count1);
        }
        return max;
    }
}
